package com.kkd.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 14587
 * @date 2020/10/28 1:08
 */
public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static{

        try {
            //获取sqlsessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //既然有了 SqlSessionFactory，顾名思义，我们可以从中获得 SqlSession 的实例。
    /**
     * @author VegetDog
     * @Description 获取连接
     * @date 2020-10-29 10:41
     * @param //这里的true指的是设置自动提交为true
     * @return org.apache.ibatis.session.SqlSession
     */
    public static SqlSession getSession(){
        return sqlSessionFactory.openSession(true);
    }
}
